﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockFinder.DataAccess;

namespace StockFinder.Services.PriceDatabaseAnalysers
{
    public class RelativeStrengthPriceDatabaseAnalyser
    {
        public void Run()
        {
            //1. get all symbols
            var symbols = SymbolDataAccess.GetAllSymbols();

            var results = new SortedDictionary<decimal, string>();

            //2. loop symbols
            foreach (var symbol in symbols)
            {
                //3. get prices
                var prices = DailyPriceDataAccess.GetAllDailyPricesBySymbol(symbol.Name);

                if (prices.Count > 0)
                {
                    //4 calculate price strength over last year
                    var c = (prices.Count > 0) ? prices[0].AdjustedClose : 0;
                    var c1 = (prices.Count > 63) ? prices[63].AdjustedClose : 0;
                    var c2 = (prices.Count > 126) ? prices[126].AdjustedClose : 0;
                    var c3 = (prices.Count > 189) ? prices[189].AdjustedClose : 0;
                    var c4 = (prices.Count > 252) ? prices[252].AdjustedClose : 0;

                    var r1 = (c1 > 0) ? (((c - c1) / c1) * .4m) : 0;
                    var r2 = (c2 > 0) ? (((c - c2) / c2) * .2m) : 0;
                    var r3 = (c3 > 0) ? (((c - c3) / c3) * .2m) : 0;
                    var r4 = (c4 > 0) ? (((c - c4) / c4) * .2m) : 0;

                    var t1 = r1 + r2 + r3 + r4;

                    var result = t1 * 100;

                    results.Add(result, symbol.Name);    
                }                
            }
        }
    }
}
